#include "insightview.h"
#include "ui_insightview.h"

#include <QLayout>
#include <QGroupBox>
#include <QRadioButton>
#include <QSettings>

InsightView::InsightView(QWidget *parent) :
    QDockWidget(parent),
    ui(new Ui::InsightView)
{
    QSettings settings;
    ui->setupUi(this);

//    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableView->setModel(&model);
    ui->tableView->installEventFilter(this);

    byteOrderGroup = new QGroupBox(tr("Byte Order"), this);
    QHBoxLayout *layout = new QHBoxLayout(byteOrderGroup);
    littleEndian = new QRadioButton(tr("Little Endian"), byteOrderGroup);
    connect(littleEndian, SIGNAL(toggled(bool)), &model, SLOT(littleEndian(bool)));
    littleEndian->setChecked(true);
    bigEndian = new QRadioButton(tr("Big Endian"), byteOrderGroup);
    layout->addWidget(littleEndian);
    layout->addWidget(bigEndian);
    byteOrderGroup->setLayout(layout);

    ui->verticalLayout->addWidget(byteOrderGroup);

#ifdef Q_OS_WIN32
    ui->tableView->setFont(settings.value("InsightFont", QFont("Courier", 10)).value<QFont>());
#else
    ui->tableView->setFont(settings.value("InsightFont", QFont("Monospace", 10)).value<QFont>());
#endif

    int _pxCharWidth;
    QFontMetrics metrics(settings.value("InsightFont", QFont("Courier", 10)).value<QFont>());
#if QT_VERSION > QT_VERSION_CHECK(5, 11, 0)
    _pxCharWidth = metrics.horizontalAdvance(QLatin1Char('2'));
#else
    _pxCharWidth = metrics.width(QLatin1Char('2'));
#endif

    int col1Width = _pxCharWidth * 14;
    ui->tableView->setColumnWidth(0, col1Width);
}

InsightView::~InsightView()
{
    delete ui;
}

void InsightView::ImsightSettingchaged()
{
    QSettings settings;
    #ifdef Q_OS_WIN32
        ui->tableView->setFont(settings.value("InsightFont", QFont("Courier", 10)).value<QFont>());
    #else
        ui->tableView->setFont(settings.value("InsightFont", QFont("Monospace", 10)).value<QFont>());
    #endif

        int _pxCharWidth;
        QFontMetrics metrics(settings.value("InsightFont", QFont("Courier", 10)).value<QFont>());
    #if QT_VERSION > QT_VERSION_CHECK(5, 11, 0)
        _pxCharWidth = metrics.horizontalAdvance(QLatin1Char('2'));
    #else
        _pxCharWidth = metrics.width(QLatin1Char('2'));
    #endif

        int col1Width = _pxCharWidth * 14;
        ui->tableView->setColumnWidth(0, col1Width);

        setTablecolumnWidth();
}

void InsightView::setData(const QByteArray &array)
{
    model.setData(array);
    update();
}

QDataStream::ByteOrder InsightView::byteOrder()
{
    return littleEndian->isChecked() ? QDataStream::LittleEndian : QDataStream::BigEndian;
}

bool InsightView::eventFilter(QObject *object, QEvent *event)
{
    if (object == ui->tableView && event->type() == QEvent::Resize) {
        setTablecolumnWidth();
    }

    return false;
}

void InsightView::setTablecolumnWidth()
{
    int width = ui->tableView->contentsRect().width();
    int col1Width = ui->tableView->columnWidth(0);
    int col2Width = width - col1Width;
    ui->tableView->setColumnWidth(1, col2Width);
}
